package com.longrise.android.byjk.plugins.aboutme;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.DZZWTools;
import com.longrise.android.byjk.utils.DialogUtil;

/**
 * 执业证书 frag
 */

public class AccountantFragmentView extends Activity {

    private TextView mText_Name;
    private TextView mText_Code;
    private TextView mText_Company;
    private TextView mText_FW;
    private TextView mText_WChart;
    private TextView mText_FZDaTe;
    private TextView mText_SDate;
    private TextView mText_EDaTe;

    private TextView mText_BackTitle;
    private RelativeLayout mRela_Back;
    private LinearLayout mLayout_BB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.fragmentview_accountantcard);
        initUI();
    }

    private void initUI() {
        mLayout_BB = (LinearLayout) findViewById(R.id.linear_bb);
        mText_Name = (TextView) findViewById(R.id.text_zy_name);
        mText_Code = (TextView) findViewById(R.id.text_zy_code);
        mText_Company = (TextView) findViewById(R.id.text_zy_com);
        mText_FW = (TextView) findViewById(R.id.text_zy_ywarea);
        mText_WChart = (TextView) findViewById(R.id.text_zy_earea);
        mText_FZDaTe = (TextView) findViewById(R.id.text_zy_date);
        mText_SDate = (TextView) findViewById(R.id.text_zy_start);
        mText_EDaTe = (TextView) findViewById(R.id.text_zy_end);
        mText_BackTitle = (TextView) findViewById(R.id.app_title);
        mText_BackTitle.setText("执业证书");
        mRela_Back = (RelativeLayout) findViewById(R.id.relapp_back);
        mRela_Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AccountantFragmentView.this.finish();
            }
        });
        try {
            clientRequest();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void clientParse(EntityBean bean) {
        if (bean != null) {
            EntityBean resultBean = (EntityBean) bean.get("result");
            String certtype = resultBean.getString("certtype");
            String certno = resultBean.getString("certno");
            String orgname = resultBean.getString("orgname");
            String scopecode = resultBean.getString("scopecodename");
            String scopeareaname = resultBean.getString("scopeareaname");
            String registertime = resultBean.getString("registertime");
            String qualistart = resultBean.getString("certstart");
            String qualiend = resultBean.getString("certend");
            mText_Name.setText(certtype);
            mText_Code.setText(certno);
            mText_Company.setText(orgname);
            mText_FW.setText(scopecode);
            mText_WChart.setText(scopeareaname);
            if (registertime.length() > 10) {
                mText_FZDaTe.setText(registertime.substring(0, 10));
            } else {
                mText_FZDaTe.setText(registertime);
            }
            if (qualistart.length() > 10) {
                mText_SDate.setText(qualistart.substring(0, 10));
            } else {
                mText_SDate.setText(qualistart);
            }
            if (qualiend.length() > 10) {
                mText_EDaTe.setText(qualiend.substring(0, 10));
            } else {
                mText_EDaTe.setText(qualiend);
            }
        }
    }

    private void clientRequest() throws Exception {
        DZZWTools.showWaitDialog(this);
        EntityBean bean = new EntityBean();
        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
        EntityBean serBean = new EntityBean();
        serBean.set("bean", bean);
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl, "bbt_user_sDevCert", serBean, new LoadDataManager.OnRequestCallListener() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                EntityBean bean = (EntityBean) result;
                if (bean != null) {
                    if (bean.getInt("resultstate") == 1) {
                        clientParse(bean);
                        mLayout_BB.setVisibility(View.VISIBLE);
                    } else {
                        hintDialog("没有查询到数据");
                    }
                }
            }

            @Override
            public void onFinished(String key, String service) {
                DZZWTools.dismissDialog(null);
            }

            @Override
            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                DZZWTools.showToast(AccountantFragmentView.this, getString(R.string.networkerror), 1000 * 2);
            }
        });
    }

    private void hintDialog(String msg) {
        View v = View.inflate(this, R.layout.dialog_connect_error, null);
        TextView text_Cancle = (TextView) v.findViewById(R.id.text_cancle_error);
        TextView textMsg = (TextView) v.findViewById(R.id.text_error);
        textMsg.setText(msg);
        final View.OnClickListener onClickListener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (v.getId() == R.id.text_cancle_error) {
                    AccountantFragmentView.this.finish();
                }
                DialogUtil.getInstance().dismiss();
            }
        };
        text_Cancle.setOnClickListener(onClickListener);
        Dialog dialog = DialogUtil.getInstance().creatAlertDialog(this, v, 266, 171);
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                    dialog.dismiss();
                    AccountantFragmentView.this.finish();
                }
                return false;
            }
        });
    }
}
